package com.freak.wanandroid.model.system;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.freak.httpmanager.BaseViewModel;
import com.freak.httpmanager.callback.ApiCallback;
import com.freak.httpmanager.callback.SubscriberCallBack;
import com.freak.wanandroid.bean.SystemClassifyBean;
import com.freak.wanandroid.bean.SystemDetailBean;
import com.freak.wanandroid.net.CommonLiveData;

import java.util.List;

public class SystemViewModel extends BaseViewModel<SystemRepository> {
    protected MutableLiveData<List<SystemClassifyBean>> mGetSystem = new MutableLiveData<>();
    protected MutableLiveData<String> mGetSystemError = new MutableLiveData<>();
    protected MutableLiveData<SystemDetailBean> mGetClassifyDetail = new MutableLiveData<>();
    protected MutableLiveData<String> mGetClassifyDetailError = new MutableLiveData<>();
    protected MutableLiveData<CommonLiveData> mDoCollect = new MutableLiveData<>();
    protected MutableLiveData<CommonLiveData> mDoCancelCollect = new MutableLiveData<>();

    public SystemViewModel(@NonNull Application application) {
        super(application);
    }

    void getSystem() {
        mRepository.getSystem(new SubscriberCallBack<>(new ApiCallback<List<SystemClassifyBean>>() {
            @Override
            public void onSuccess(List<SystemClassifyBean> model) {
                mGetSystem.postValue(model);
            }

            @Override
            public void onFailure(String msg) {
                mGetSystemError.postValue(msg);
            }
        }));
    }

    void getClassifyDetail(String curPage, String cid) {
        mRepository.getClassifyDetail(curPage, cid, new SubscriberCallBack<>(new ApiCallback<SystemDetailBean>() {
            @Override
            public void onSuccess(SystemDetailBean model) {
                mGetClassifyDetail.postValue(model);
            }

            @Override
            public void onFailure(String msg) {
                mGetClassifyDetailError.postValue(msg);
            }
        }));
    }

    void doCancelCollect(int id) {
        mRepository.doCancelCollect(id, new SubscriberCallBack<>(new ApiCallback<String>() {
            @Override
            public void onSuccess(String model) {
                mDoCancelCollect.postValue(new CommonLiveData(CommonLiveData.SUCCESS, model));
            }

            @Override
            public void onFailure(String msg) {
                mDoCancelCollect.postValue(new CommonLiveData(CommonLiveData.ERROR, msg));
            }
        }));
    }

    void doCollect(int id) {
        mRepository.doCollect(id, new SubscriberCallBack<>(new ApiCallback<String>() {
            @Override
            public void onSuccess(String model) {
                mDoCollect.postValue(new CommonLiveData(CommonLiveData.SUCCESS, model));
            }

            @Override
            public void onFailure(String msg) {
                mDoCollect.postValue(new CommonLiveData(CommonLiveData.ERROR, msg));
            }
        }));
    }
}
